import message.*;
import org.apache.commons.lang3.builder.ToStringBuilder;
import serialport.ByteUtils;
import serialport.SerialPortData;
import serialport.SerialPortException;
import tools.FileUtil;

import java.util.List;

public class Main {
    public static void main(String[] args) throws SerialPortException {


        Main main = new Main();

//        int decimal = 17;
//        String hex = Integer.toHexString(decimal);
//        System.out.println("十六进制表示: " + hex);

//        String hexString = "50";
//        int decimal = Integer.parseInt(hexString, 16);
//        System.out.println("十六进制数: " + hexString + " 转换为十进制是: " + decimal);

        //5口柜机上报数据（模拟16进制）
        main.receiveUpload5Test();

        //弹出第1个机芯，第6个孔位
//        main.sendPopupIndexTest();

        //模拟孔位弹出失败
//        main.rseceivePopupIndexTest();

        //弹出第1个机芯，SN：1760771
//        main.sendPopupSNTest();

        //模拟弹出SN成功
//        main.receivePopupSNTest();

        //模拟充电宝归还
//        main.receiveReturnTest();
    }

    //5口柜机上报数据（模拟16进制）
    public void receiveUpload5Test() throws SerialPortException {
//        String data = "A8 00 56 10 10 00 00 64 66 01 01 01 00 00 01 01 31 4E 71 64 00 32 0F 03 01 02 01 00 00 01 01 31 3C 4B 64 00 32 0F 03 01 03 01 00 00 01 01 31 43 AA 64 00 32 0F 03 01 04 00 00 00 00 00 00 00 00 FF FF FF FF 00 00 05 00 00 00 00 00 00 00 00 FF FF FF FF 00 00 46";
        String data = "A8 00 C5 10 10 00 00 00 0D 01 01 00 00 00 00 00 00 00 00 FF FF FF FF 00 00 02 00 00 00 00 00 00 00 00 FF FF FF FF 00 00 03 00 00 00 00 00 00 00 00 FF FF FF FF 00 00 04 00 00 00 00 00 00 00 00 FF FF FF FF 00 00 05 00 00 00 04 00 00 00 00 FF FF FF FF 00 00 06 01 00 00 04 00 98 98 19 64 1A 32 00 01 01 60 00 00 00 0D 01 1F 00 00 00 00 00 00 00 00 FF FF FF FF 00 00 20 00 00 00 00 00 00 00 00 FF FF FF FF 00 00 21 00 00 00 00 00 00 00 00 FF FF FF FF 00 00 22 00 00 00 04 00 00 00 00 FF FF FF FF 00 00 23 00 00 00 04 00 00 00 00 FF FF FF FF 00 00 24 00 00 00 04 00 00 00 00 FF FF FF FF 00 00 35";
        System.out.println(data);

        byte[] bytes = ByteUtils.toBytes(data);
        ReceiveUpload uploadMessage = new ReceiveUpload(bytes);

        List<Powerbank> powerbanks = uploadMessage.getPowerbanks();
        for(Powerbank item : powerbanks){
            System.out.println(ToStringBuilder.reflectionToString(item));
        }
    }

    //弹出第2个机芯，第6个孔位
    public void sendPopupIndexTest() throws SerialPortException {
        SendPopupIndex sendPopupIndex = new SendPopupIndex(0x20, 0x06);

        String data = ByteUtils.to16Hexs(sendPopupIndex.getBytes());
        System.out.println(data);
        System.out.println(ToStringBuilder.reflectionToString(sendPopupIndex));
    }

    //模拟孔位弹出失败
    public void rseceivePopupIndexTest() throws SerialPortException {
        SerialPortData serialPortData = new SerialPortData(0x21, new int[]{0x20,0x06,0x00});

        ReceivePopupIndex receivePopupIndex = new ReceivePopupIndex(serialPortData.getBytes());

        String data = ByteUtils.to16Hexs(receivePopupIndex.getBytes());
        System.out.println(data);
        System.out.println(ToStringBuilder.reflectionToString(receivePopupIndex));
    }

    //弹出第1个机芯，SN：1760771
    public void sendPopupSNTest() throws SerialPortException {
        //0x00 1A DE 03 = 1760771
        SendPopupSN sendPopupSN = new SendPopupSN(0x10, new int[]{0x00, 0x1A, 0xDE, 0x03});

        String data = ByteUtils.to16Hexs(sendPopupSN.getBytes());
        System.out.println(data);
        System.out.println(ToStringBuilder.reflectionToString(sendPopupSN));
    }

    //模拟弹出SN成功
    public void receivePopupSNTest() throws SerialPortException {
        //0x00 1A DE 03 = 1760771
        SerialPortData serialPortData = new SerialPortData(0x31, new int[]{0x10, 0x00, 0x1A, 0xDE, 0x03, 0x01});
        ReceivePopupSN receivePopupSN = new ReceivePopupSN(serialPortData.getBytes());

        String data = ByteUtils.to16Hexs(receivePopupSN.getBytes());
        System.out.println(data);
        System.out.println(ToStringBuilder.reflectionToString(receivePopupSN));
    }

    //模拟充电宝归还.SN:20010060
    public void receiveReturnTest() throws SerialPortException {
        byte[] bytes = ByteUtils.toBytes(0XA8,0X00,0X0E,0X40,0X20,0X06,0X01,0X01,0X31,0X54,0X4C,0X01,0X04,0X0C);

        ReceiveReturn receiveReturn = new ReceiveReturn(bytes);

        String data = ByteUtils.to16Hexs(receiveReturn.getBytes());
        System.out.println(data);
        System.out.println(ToStringBuilder.reflectionToString(receiveReturn));
    }





}
